//
//  HNNetworkManager.m
//  HAFund
//
//  Created by 夏成龙 on 2018/10/29.
//  Copyright © 2018年 夏成龙. All rights reserved.
//

#import "HANetworkManager.h"
#import <AFNetworking.h>
#import "HANetworkRequest.h"
#import "HAResponse.h"

@interface HANetworkManager ()

@property (strong, nonatomic) AFHTTPRequestSerializer *httpRequestSerializer;
@property (copy, nonatomic) NSString *requestHost;

@end

@implementation HANetworkManager

+ (instancetype)manager {
    static HANetworkManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[HANetworkManager alloc] init];
    });
    return manager;
}

///网络是否链接
+ (BOOL)isReachable {
   return [AFNetworkReachabilityManager manager].isReachable;
}

///网络是否是WiFi
+ (BOOL)isReachableViaWiFi {
    return [AFNetworkReachabilityManager manager].isReachableViaWiFi;
    
}

///网络是否为移动网络
+ (BOOL)isReachableViaWWAN {
     return [AFNetworkReachabilityManager manager].isReachableViaWWAN;
}


+ (HARequest *)GETWithPath:(NSString *)path requestParams:(id)requestParams success:(void(^)(NSDictionary *response))success failure:(void(^)(NSError *error))failure {
    if (path == nil) {
        return nil;
    }
    
    HARequest *request = [HARequest requestWithURLString:path params:requestParams type:HARequestTypeGET];
    [HANetworkRequest callRequest:request callBlock:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
       HAResponse *responseModel = [[HAResponse alloc] initWithResponse:response responseObject:responseObject error:error];
        
        if (responseModel.error && failure) {
            failure(responseModel.error);
        } else if (responseModel.responseObject && success) {
            success(responseModel.responseObject);
        }
    }];
    return request;
}

+ (HARequest *)POSTWithPath:(NSString *)path requestParams:(id)requestParams success:(void (^)(NSDictionary *))success failure:(void (^)(NSError *))failure {
    if (path == nil) {
        return nil;
    }
    
    HARequest *request = [HARequest requestWithURLString:path params:requestParams type:HARequestTypePOST];
    [HANetworkRequest callRequest:request callBlock:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
        HAResponse *responseModel = [[HAResponse alloc] initWithResponse:response responseObject:responseObject error:error];
        
        if (responseModel.error && failure) {
            failure(responseModel.error);
        } else if (responseModel.responseObject && success) {
            success(responseModel.responseObject);
        }
    }];
    return request;
}
@end
